﻿
namespace CaliburnWpf.ViewModels.Dictionaries
{
    using MoneyManagerNET.Model;
    using MoneyManagerNET.Presentation.ViewModel.Base;
    using Caliburn.Micro;


    public class ContragentListViewModel:ListViewModel<IContragent>//ViewModelBase
    {
        public ContragentListViewModel()
            : base(App.Storage.Select<IContragent>())
        {
        }

        string _text;
        public string Text { 
            get { return _text; }
            set { _text = value; this.NotifyOfPropertyChange(() => Text); this.NotifyOfPropertyChange(()=>CanNew); } 
        }



        protected override void Moved()
        {
            Text = ActiveItem== null ? string.Empty : ActiveItem.Name;

            base.Moved();
        }

        protected override void RefreshCommandStatuses()
        {
            base.RefreshCommandStatuses();

            NotifyOfPropertyChange(()=>CanNew);
            NotifyOfPropertyChange(() => CanEdit);
        }


        public bool CanNew { get { return !string.IsNullOrEmpty(Text); } }
        public void New() {
            var c=App.Storage.New<IContragent>();
            c.Name = Text; 
            Items.Add(c);

            RefreshCommandStatuses();
        }

        public bool CanEdit { get { return ActiveItem != null; } }
        public void Edit()
        {
            var contragent = base.ActiveItem;

            if (contragent != null)
            {
                contragent.Name = this.Text;
                //RaisePropertyChanged(contragent,"Name");//TODO:
            }
        }

        public void Select() {
            this.TryClose(true);
        }


        
    }
}
